package com.jr.mapper;

import com.jr.pojo.Task;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
public interface TaskMapper {

    @Select("SELECT * FROM tasks WHERE task_id = #{taskId}")
    Task selectById(Integer taskId);

    @Select("SELECT * FROM tasks WHERE user_id = #{userId} ORDER BY due_date ASC")
    List<Task> selectByUserId(Integer userId);

    @Select("SELECT * FROM tasks WHERE user_id = #{userId} AND is_completed = #{isCompleted} ORDER BY due_date ASC")
    List<Task> selectByUserIdAndStatus(@Param("userId") Integer userId, @Param("isCompleted") Boolean isCompleted);

    @Select("SELECT * FROM tasks WHERE user_id = #{userId} AND due_date <= #{dueDate} AND is_completed = false ORDER BY due_date ASC")
    List<Task> selectOverdueTasks(@Param("userId") Integer userId, @Param("dueDate") LocalDateTime dueDate);

    @Insert("INSERT INTO tasks (user_id, title, description, type, due_date, is_completed, priority, created_at, updated_at) " +
            "VALUES (#{userId}, #{title}, #{description}, #{type}, #{dueDate}, #{isCompleted}, #{priority}, #{createdAt}, #{updatedAt})")
    @Options(useGeneratedKeys = true, keyProperty = "taskId")
    int insertTask(Task task);

    @Update("UPDATE tasks SET title=#{title}, description=#{description}, type=#{type}, due_date=#{dueDate}, " +
            "is_completed=#{isCompleted}, priority=#{priority}, updated_at=#{updatedAt} WHERE task_id=#{taskId}")
    int updateTask(Task task);

    @Update("UPDATE tasks SET is_completed = #{isCompleted}, updated_at = #{updatedAt} WHERE task_id = #{taskId}")
    int updateTaskStatus(@Param("taskId") Integer taskId, @Param("isCompleted") Boolean isCompleted, @Param("updatedAt") LocalDateTime updatedAt);

    @Delete("DELETE FROM tasks WHERE task_id = #{taskId}")
    int deleteTask(Integer taskId);
}
